# coding=utf-8 
# @Author: Chaomin
# @time: 2021/12/29 下午9:13
# @File: 10-01 stake data.py
# @Software: PyCharm

"""
【课题】：Python 股票数据采集并做数据可视化
【目标】：爬取雪球平台的股票数据
【三方库】：requests
【步骤】：
    1、确定浏览器要访问URL：
        https://xueqiu.com/hq#exchange=CN&plate=1_3_2
        &firstName=1&secondName=1_3
        &type=sha&order=desc&order_by=amount

        确认数据存放的URL：
        https://xueqiu.com/service/v5/stock/screener/quote/
        list?page=1&size=30&order=desc&order_by=amount
        &exchange=CN&market=CN&type=sha&_=1640873723450

    2、发送网络请求(发送的请求需要进行伪装）
    3、数据解析
    4、数据保存（数据库（mysql\mongodb\redis)

"""

import requests  # 发送网络请求
import pandas as pd

# file = open('data2.csv', encoding='utf-8', newline='')
# csv_write = csv.DictWriter(file, fieldnames=['股票代码', '股票名称', '当前价格', '2021年涨幅', '市盈率', '股息率'])
# csv_write.writeheader()

"""获取网页内容"""
column_list = ['股票代码', '股票名称', '当前价格', '2021年涨幅', '市净率', '市盈率', '股息率']
data_df = pd.DataFrame(data=None, index=None, columns=None)
for page in range(1, 57):
    url01 = 'https://xueqiu.com/service/v5/stock/screener/quote/list' \
            '?page=%d&size=30&order=desc&order_by=amount' \
            '&exchange=CN&market=CN&type=sha&_=1640873723450' % page

    # 直接requests.get，被服务器判断为爬虫，返回，<Response [403]>, 是拿不到数据的。
    # 需要加一个headers进行伪装；

    head = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) '
                          'AppleWebKit/537.36 (KHTML, like Gecko) '
                          'Chrome/96.0.4664.110 Safari/537.36'}

    # 加上之后，requests.get返回 <Respons [200]>
    data_object = requests.get(url01, headers=head)

    """获取所需数据"""
    data_packet = data_object.json()
    data_list = data_packet['data']['list']

    ln = 1 + (page - 1) * 30

    for data in data_list:
        symbol = data['symbol']
        name = data['name']
        price_current = data['current']
        anual_percent = data['current_year_percent']
        pb = data['pb']
        pe_ttm = data['pe_ttm']
        dividend = data['dividend_yield']
        df_stake = pd.DataFrame(data=[[symbol, name, price_current, anual_percent, pb, pe_ttm, dividend]], index=[ln],  columns=column_list)
        data_df = data_df.append(df_stake)
        ln += 1
    # csv_write.writerow(data_dict)
# file.close()
print(data_df)
data_df.to_excel('2021沪市股票总结.xlsx')





